<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\CommentModel;
use think\Request;
use think\View;
use app\common\model\UserModel;

class Comment extends Frontend
{
	//获取评论
	public function index(Request $request = null){
		$param = [
			'article_id' => $request->param("article_id/d",0),
			'page' => $request->param("page/d",1),
		];

		$data = (new CommentModel())->getCommentByArticeId($param);
		return json(['status'=>200,'msg'=>'成功','data'=>$data]);
	}


	//评论
	public function comment(Request $request = null){
		$token = $request->param("token","");
		$UserModel = new UserModel();
		$user = $UserModel->getUserByToken($token);
		if(empty($user)){
			return json(['status'=>400,'msg'=>'找不到该用户！']);
		}

		$param = [
			'uid' => $user['id'],
			'comment' => $request->param("comment","strip_tags,htmlspecialchars"),
			'article_id' => $request->param("article_id/d",0),
		];

		// var_dump( $param   );die();

		$result = (new CommentModel())->comment($param);
		if($result > 0){
			return json(['status'=>200,'msg'=>'成功','data'=>[]]);
		}else{
			return json(['status'=>400,'msg'=>'失败','data'=>[]]);
		}
		
	}
}